const cookieParser = process.server ? require("cookieparser") : undefined


//在服务端渲染期间运行的是同一个实例
//为了防止数据冲突，务必要把 state 定义成一个函数，返回数据对象

export const state = () => {
   return {
       //当前登录用户的登录状态
       user:null
   }
}

export const mutations = {
   setUser (state,data) {
       console.log(data)
       state.user = data
   }
}
export const actions = {
    nuxtServerInit ({commit},{req}) {
        let user = null
        if(req.headers.cookie){
            const parsed = cookieParser.parse(req.headers.cookie)
            try {
                user = JSON.parse(parsed.user)
            } catch (err) {

            }
        }
        commit("setUser",user)
    }
 }